// pages/index/topUp/index.js
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    moneyList: [
      {
        comboId: 0,
        realAmount: '10',
        addAmount: '5',
        amount: ''
      }
    ],
    // 当前余额
    money: 0,
    // 应付金额
    payMoney: 0,
    // 被选中的套餐标识(实付金额)
    itemMoney: 0,
    // 呼叫器id
    beeperId: 0,
    // 到账金额
    amount: 0,
    // 套餐id
    comboId: 0,

    // 待支付订单
    shopList: [],
    shopShow: false,
    shopIds: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    if (options) {
      this.setData({
        beeperId: options.id
      });
    }
  },

  // 打开选择商户
  onShopOpen() {
    this.setData({ shopShow: true });
  },
  // 关闭选择商户
  onShopClose() {
    this.setData({ shopShow: false });
  },
  // 选择商户
  onShopChange(event) {
    this.setData({
      shopIds: event.detail
    });
  },

  // 确认商户
  onShopConfirm() {
    let list = this.data.shopIds;
    // 先转成数字
    for (let i = 0; i < list.length; i++) {
      list[i] = Number(list[i]);
    }
    let busIds = this.data.shopIds;
    let params = {
      beeperId: Number(this.data.beeperId),
      orderIds: busIds
    };
    console.log(params);
    $api.beeperBalancePay(params).then(res => {
      if (res.data.status == 0) {
        setTimeout(() => {
          Toast.success('支付成功');
          this.setData({ shopShow: false });
          this.getBeeperBalance();
        }, 1000);
      }
    });
  },

  // 点击套餐
  clickItem(e) {
    // 	赠送金额
    let addAmount = e.currentTarget.dataset.addamount;
    // 到账金额
    let amount = e.currentTarget.dataset.amount;
    // 支付金额
    let realAmount = e.currentTarget.dataset.realamount;
    // 套餐id
    let comboId = e.currentTarget.dataset.comboid;
    this.setData({
      itemMoney: addAmount,
      amount: amount,
      payMoney: realAmount,
      comboId: comboId
    });
  },
  // 跳转充值记录
  goRecord() {
    wx.navigateTo({
      url: '../record/index'
    });
  },
  // 查询余额
  getBeeperBalance() {
    let params = {
      id: this.data.beeperId
    };
    $api
      .getBeeperBalance(params)
      .then(res => {
        this.setData({
          money: res.data.data
        });
      })
      .catch(err => {
        console.log('查询呼叫器余额报错', err);
      });
  },
  // 查询套餐
  comboInfo() {
    $api
      .comboInfo()
      .then(res => {
        console.log('充值套餐', res);
        this.setData({
          moneyList: res.data.data
        });
        console.log('充值套餐', this.data.moneyList);
      })
      .catch(err => {
        console.log('充值套餐报错', err);
      });
  },
  // 查询未支付的订单
  getPayOrder() {
    var that = this;
    let params = {
      beeperId: that.data.beeperId
    };
    $api.listWaitPayOrder(params).then(res => {
      if (res.data.data.length != 0) {
        that.setData({
          shopList: res.data.data,
          shopShow: true
        });

        console.log(that.data.shopList);
      }
    });
  },
  // 点击充值
  payMent() {
    // 实付金额
    let payMoney = this.data.payMoney;
    var _this = this;
    if (payMoney == 0) {
      Toast.fail('请先选择充值套餐');
      return;
    }
    let params = {
      beeperId: Number(this.data.beeperId),
      comboId: this.data.comboId,
      openId: wx.getStorageSync(Storage.USER_OPEN_ID),
      payType: 1
    };
    $api
      .rechargePay(params)
      .then(res => {
        let data = res.data.data;
        // 唤起支付
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success(res) {
            console.log('充值成功回调', res);
            // 充值成功后查询是否支付未支付的订单
            _this.getPayOrder();
          },
          fail(res) {
            console.log('充值fail回调', res);
          },
          complete(res) {
            console.log('充值complete回调', res);
          }
        });
        _this.getBeeperBalance();
      })
      .catch(err => {
        console.log('充值报错', err);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBeeperBalance();
    this.comboInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
